package com.zhiyou.utils;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

	public static void sendEmail(String email,String emailMsg) throws MessagingException{
		//新建一个属性文件对象
		Properties properties = new Properties();
		//发送服务器需要身份验证
		properties.setProperty("mail.smtp.auth", "true");
		//发送端口
		properties.setProperty("mail.smtp.port", "25");
		//设置邮件服务器主机名
		properties.setProperty("mail.smtp.host", "smtp.qq.com");
		//设置发送邮件协议名称
		properties.setProperty("mail.transport.protocol", "smtp");
		
		//根据配置创建对话对象，用于和邮件服务器进行交互
		Session session = Session.getInstance(properties);
		//创建邮件对象
		Message message = new MimeMessage(session);
		//设置邮件主题
		message.setSubject("邮箱身份验证");
		//编写邮箱内容
		message.setText(emailMsg);
		
		//设置发送者
		message.setFrom(new InternetAddress("1437652219@qq.com"));
		//发送邮件
		Transport transport = session.getTransport();
		transport.connect("smtp.qq.com", "1437652219@qq.com", "jxujghclpqjvgdeb");
		 transport.sendMessage(message,new Address[] { new InternetAddress(email)});
		 transport.close();
	}
}
